package com.example.websockettest.webSocket.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class Sentence {
    public static Map<String,Object> wordToShow(String sentence)  {
        String[] sentenceBySplit = sentence.split("\\s+");
        HashMap<Integer, String> getPunctuation = new HashMap<>();
        for (int i = 0; i < sentenceBySplit.length; i++) {
            if (sentenceBySplit[i].endsWith(",") || sentenceBySplit[i].endsWith(".")) { // 判断单词是否以逗号或句号结尾
                if (sentenceBySplit[i].endsWith(",")) {
                    getPunctuation.put(i, ",");
                }
                else {
                    getPunctuation.put(i, ".");
                }
                // 去除标点符号
                sentenceBySplit[i] = sentenceBySplit[i].substring(0, sentenceBySplit[i].length() - 1);
            }
        }
        Random random = new Random();
        int index = random.nextInt(sentenceBySplit.length);
        String rightWord = sentenceBySplit[index];
        sentenceBySplit[index] = "_____";
        Set<Integer> keys = getPunctuation.keySet();
        //恢复标点符号
        for (Integer key : keys){
            sentenceBySplit[key] += getPunctuation.get(key);
        }
        String sentenceToShow = String.join(" ", sentenceBySplit);
        HashMap<String, Object> getSentenceInfo = new HashMap<>();

        getSentenceInfo.put("sentence",sentence);
        getSentenceInfo.put("sentenceToShow", sentenceToShow);
        getSentenceInfo.put("rightWord",rightWord);

        return getSentenceInfo;

    }
}
